import React, {Component} from 'react';
import HouseItem from '../../../components/HoustItem/HouseItem';
import { getToken } from '../../../utiles/token';
import {get} from '../../../netWork';
import NavHeader from '../../../components/NavHeader/NavHeader';
import {Modal} from "antd-mobile";
import {patch} from "../../../netWork";

const operation = Modal.operation;
class Rented extends Component{
    state = {
        myhouse: [],
        status:null
    };

    componentDidMount() {
        this.getMyHouse()
    }
    //获取已发布的房源
    getMyHouse = () =>{
        var value = getToken()
        get({
            url:'/user/houses',
            headers:{authorization:value }
        }).then(res =>{
            // console.log(res);
            if(res.status === 400){
                this.setState({status:res.status})
            }else if(res.status === 200){
                this.setState({myhouse:res.body})
            }
        })
    };
    //删除已发布房屋
    onDelete = (el) => {
        operation([
            { text: '刪除', onPress: () => this.DeleteHouse(el) },
            { text: '详情', onPress: () => this.houseDetail(el) },
        ])
    };
    houseDetail = (el) =>{
        this.props.history.push(`/housedetail/${el}`)
    }
    DeleteHouse = (el) =>{
        let token = getToken();
        console.log(el);
        patch({
            url:`/user/houses/${el}`,
            headers: {authorization :token},
            data:{ "shelf": true}
        }).then(res =>{
            this.getMyHouse()
        })
    };
    render(){
        return (
            <div>
                   <NavHeader>已发布的房源</NavHeader>
                    <HouseItem houseList={this.state.myhouse} onDelete = {this.onDelete}/>
            </div>

        )
    }
}

export default Rented
